// pages/warehouseManage/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    warehouseId: '', // 仓库ID
    areaId: '', // 库区ID
    warehouseName: '',
    areaName: '',
    handType:"in",

    /**
       * 选择储存库、库区
    */
    wareFlag: false, // 弹层开关
    wareList: [],

    wareNames: {
      text: 'warehouseName',
      value: 'id'
    },

    totalPage: 0, // 总页数
    queryParams: {
      page: 1,
      size: 10,
    },
    pullFlag: false, // 是否向上拉动到底部
    tableList: [],
    listTip: '上拉加载更多'
  },


  onShow() {
    this.getList()
  },

  onReady () {
    // 请求储存库/库区数据
    wx.$req({
      url: '/wms/warehouse/tree',
    }).then(res => {

      // 不要根节点
      // 并且， 没有库区的仓库要设置禁选
      const result = res.data[0].children
      result.forEach(item => {
        if (item.children.length === 0) {
          item.disabled = true
        }
      })
      this.setData({  wareList: result })
    })
  },
  onChange(e){
    let handtype=e.detail.name
    this.setData({
      handType:handtype,
      areaId: '',
      warehouseId: '',
      warehouseName: '',
      areaName: '',
    }, () => { 
      this.getList()
    })
  },

  // 关闭弹窗，同时清空内容
	onCloseWare () {
		this.setData({ wareFlag: false })
	},

  /* 储存库、库区 级联选择相关 */
	// 打开级联选择弹窗
	openWare () {
		this.setData({  wareFlag: true })
	},

	// 级联选择完毕
	onFinishWare (e) {
    const result = e.detail.selectedOptions

		let warehouseName = result[0].warehouseName,
		areaName = result[1].warehouseName,
		warehouseId = result[0].id,
    areaId = result[1].id

   
    this.setData({
      areaId,
      warehouseId,
      warehouseName,
      areaName
    }, () => { 
      this.onCloseWare()

      this.getList()
    })

  },

  // 清空
  clearHandle() {
    this.setData({
      areaId: '',
      warehouseId: '',
      warehouseName: '',
      areaName: '',
    }, () => { 
      this.getList()
    })
  },
  
  /**
	* 页面相关事件处理函数--监听用户下拉动作
	*/
	onPullDownRefresh() {
		const queryParams = { page: 1,  size: 10 }

		this.setData({ 
			pullFlag: false,
			queryParams
		})

		this.getList()
	},
	
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    let { page, size } = this.data.queryParams

    if (page < this.data.totalPage) {

      page += 1
      this.setData({  
        pullFlag: true,
        queryParams: { page,  size },
      })
      this.getList()
    }
  },
		
  // 列表请求
  getList() {
    this.setData({ listTip: '加载中...' })
    const { page, size } = this.data.queryParams
    let url=""
    if(this.data.handType=="in"){//入库
        url="/wms/transactions/into/list"
    }else{//出库
      url="/wms/transactions/out/list"
    }
    // 获取列表数据
    wx.$req({
      url: url+`?page=${page - 1}&size=${size}`,
      method: 'post',
      loadingText: '加载中',
      data: {
        warehouseId: this.data.warehouseId, // 仓库ID
        areaId: this.data.areaId, // 库区ID
      }
    }).then(res => {
      
      let totalPage = res.data.totalPages
      let rows = this.data.tableList

      if (totalPage == 1 || this.data.totalPage === totalPage ) {
        this.setData({ listTip: '暂无更多' })
      } else {
        this.setData({ listTip: '上拉加载更多' })
      }

      if (totalPage == 0) {
        this.setData({ listTip: '暂无内容' })
      }
    
      // 列表数据：为true 则需要push, false，则直接覆盖
      if (this.data.pullFlag) {
        rows.push(...res.data.content)
      } else {
        rows = res.data.content
      }

      this.setData({ 
        tableList: rows,
        totalPage
      })
    }).finally(() => { wx.stopPullDownRefresh() })
  },
// 跳转添加
  addWarehouse() {
    wx.navigateTo({
      url: '/pages/addWarehouse/index?type='+this.data.handType,
    })
  },
// 跳转详情
  toDetail (e) {
    const info = JSON.stringify(e.currentTarget.dataset.info)
   
    wx.navigateTo({
      url: '/pages/addWarehouseDetail/index?type='+this.data.handType,
      success: function(res) {
        res.eventChannel.emit('info', { data: info })
      }
    })
  }

})